package com.ehouse.fragments;


import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.CursorAdapter;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.ehouse.R;
import com.ehouse.ScheduleDetailActivity;
import com.ehouse.adapters.ScheduleIrrigationItemAdapter;
import com.ehouse.database.IrrigationScheduleTable;
import com.ehouse.database.LightScheduleTable;

public class ScheduleIrrigationsFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor>{


	// This is the Adapter being used to display the list's data.
	ScheduleIrrigationItemAdapter mAdapter;

    // If non-null, this is the current filter the user has provided.
    String mCurFilter;

    @Override public void onActivityCreated(Bundle savedInstanceState) 
    {
        super.onActivityCreated(savedInstanceState);

        // Give some text to display if there is no data.  In a real
        // application this would come from a resource.
        setEmptyText(getResources().getString(R.string.no_schedule));
        // Start out with a progress indicator.
        setListShown(false);

        getLoaderManager().initLoader(0, null, this);
    }
    
    public void reload(){
    	setListShown(false);
    	getLoaderManager().restartLoader(0, null, this);

		
    }
    
    static final String[] PROJECTION = new String[] { IrrigationScheduleTable.COLUMN_ID, IrrigationScheduleTable.COLUMN_OBJ_ID, IrrigationScheduleTable.COLUMN_STATE, IrrigationScheduleTable.COLUMN_TIME };
	public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
		return new CursorLoader(getActivity(), IrrigationScheduleTable.CONTENT_URI,
                PROJECTION, null, null,null);
	}

	public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
       mAdapter = new ScheduleIrrigationItemAdapter(getActivity(), R.layout.schedule_item, data, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
       setListAdapter(mAdapter);
       mAdapter.swapCursor(data);
       setListShown(true);
	}

	public void onLoaderReset(Loader<Cursor> arg0) {
		 mAdapter.swapCursor(null);
	}
	

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		Intent detailIntent = new Intent(v.getContext(), ScheduleDetailActivity.class);
		String time =  ((TextView)v.findViewById(R.id.scheduleLabel)).getText().toString();
		String detail =((TextView) v.findViewById(R.id.detailLabel)).getText().toString();
		Cursor cursor = mAdapter.getCursor();
		
		detailIntent.putExtra("objId", cursor.getString(cursor.getColumnIndex(LightScheduleTable.COLUMN_OBJ_ID)));
		detailIntent.putExtra("time",time);
		detailIntent.putExtra("detail", detail);
		detailIntent.putExtra("type", 2);
		
		startActivityForResult(detailIntent, 2);
	 }

}
